﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Entity.Admin;
using Web.Framework;
using Service;

namespace Web.Areas.Admin.Controllers
{
    public class MessageController : BaseController
    {
        MessageService messageService;
        public MessageController()
        {
            messageService = new MessageService(db);
        }
        //
        // GET: /Admin/Message/

        public ViewResult Index()
        {
            return View();
        }

        public ActionResult UserReceivedMessages()
        {
            var messages = messageService.GetUserReceivedMessagesByUserName(this.User.Identity.Name);
            return View(messages.ToList());
        }

        public ActionResult UserSendMessages()
        {
            var messages = messageService.GetUserSendMessagesByUserName(this.User.Identity.Name);
            return View(messages.ToList());
        }

        //
        // GET: /Admin/Message/Details/5

        public ViewResult Details(string id)
        {
            Message message = db.Messages.Find(id);
            return View(message);
        }

        //
        // GET: /Admin/Message/Create

        public ActionResult Create()
        {
            ViewBag.RefId = new SelectList(db.Messages, "ID", "Sender_ID");
            return View();
        }

        //
        // POST: /Admin/Message/Create

        [HttpPost]
        public ActionResult Create(Message message)
        {
            if (ModelState.IsValid)
            {
                db.Messages.Add(message);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.RefId = new SelectList(db.Messages, "ID", "Sender_ID", message.RefId);
            return View(message);
        }

        //
        // GET: /Admin/Message/Edit/5

        public ActionResult Edit(string id)
        {
            Message message = db.Messages.Find(id);
            ViewBag.RefId = new SelectList(db.Messages, "ID", "Sender_ID", message.RefId);
            return View(message);
        }

        //
        // POST: /Admin/Message/Edit/5

        [HttpPost]
        public ActionResult Edit(Message message)
        {
            if (ModelState.IsValid)
            {
                db.Entry(message).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.RefId = new SelectList(db.Messages, "ID", "Sender_ID", message.RefId);
            return View(message);
        }

        //
        // GET: /Admin/Message/Delete/5

        public ActionResult Delete(string id)
        {
            Message message = db.Messages.Find(id);
            return View(message);
        }

        //
        // POST: /Admin/Message/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(string id)
        {
            Message message = db.Messages.Find(id);
            db.Messages.Remove(message);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}